﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Data;
using System.Collections;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class PS_HisEnglishTest : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        private IHisEnglishBLL engBLL = new HisEnglishBLL();
        private object _dataItem = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            lblEmpSequence.Text = Session["emp_seq"].ToString();

            if (DataItem != null)
            {
                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    lblSequence.Text = DataBinder.Eval(DataItem, "ENG_SEQ").ToString();
                    txtInsName.Text = DataBinder.Eval(DataItem, "INS_NAME").ToString();
                    txtRemarks.Text = DataBinder.Eval(DataItem, "REMARKS").ToString();
                    txtStartDate.SetValue(DataBinder.Eval(DataItem, "DATE_START"));
                }

                btnUpdate.Visible = !(DataItem is Telerik.Web.UI.GridInsertionObject);
                btnAddnew.Visible = (DataItem is Telerik.Web.UI.GridInsertionObject);
            }
        }

        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }
        
        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            btnAddnew.CommandName = "Update";

            if (string.IsNullOrEmpty(txtStartDate.GetStringValue()))
            {
                btnAddnew.CommandName = "btnClick";
                ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "onclick", "MessageAlert('กรุณากรอกวันที่')", true);
            }
            else
            {
                engBLL.Insert(Int32.Parse(lblEmpSequence.Text)
                    , txtInsName.Text.Trim()
                    , DateTime.Parse(txtStartDate.GetStringValue())
                    , (DateTime?)null
                    , txtRemarks.Text.Trim()
                    , ssMng.EmpSeqI);                
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            btnUpdate.CommandName = "Update";

            if (string.IsNullOrEmpty(txtStartDate.GetStringValue()))
            {
                btnUpdate.CommandName = "btnClick";
                ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "onclick", "MessageAlert('กรุณากรอกวันที่')", true);
            }
            else
            {
                engBLL.Update(Int32.Parse(lblSequence.Text)
                    , txtInsName.Text.Trim()
                    , DateTime.Parse(txtStartDate.GetStringValue())
                    , (DateTime?)null
                    , txtRemarks.Text.Trim()
                    , ssMng.EmpSeqI);
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }
    }
}